//常用功能
app.service('common', function($rootScope, $http, $location, $timeout, $sce){
    var that    = this;

    //信任HTML
    this.trustAsHtml    = function(type, info, isList){
        switch(type){
            case 'activity':
                if(isList == false){
                    info.introduct  = $sce.trustAsHtml(info);
                }
            break;
        }

        // return info;
    }

    //回车键监听回调
    this.listenEnterKey     = function(callback){
        document.onkeydown=function(event){
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if(e && e.keyCode==13){
                callback();
            }
        };
    }
    
    //创建富文本框
    this.createEditor   = function(id){
        var editor  = new wangEditor(id);
        //文本配置
        editor.config.uploadImgUrl  = config.prefix_url+'/Public/upload?type=wangeditor';
        editor.config.uploadImgFileName     = 'Filedata';
        editor.config.menus     = [
            'source', '|', 'bold', 'underline', 'italic', 'forecolor', 'fontsize',
            'unorderlist', 'alignleft', 'aligncenter', 'alignright', 'img', '|', 'fullscreen'
        ];
        editor.create();

        return editor;
    }

    //加载菜单
    this.loadMenu   = function(){
        var url     = config.prefix_url+'/Public/getMenuList';
        $http.get(url).success(function(info){
            if(info.code != 1){
                that.dealError(info);
                return false;
            }

            $rootScope.menus    = info.data.list;
            $timeout(function(){
                //导航展开效果
                $('.s-list-group').on('show.bs.collapse', function () {
                    $(this).prev('.list-group-item').addClass('active');

                    $(this).prev('.list-group-item').find('.icon-sys')
                        .removeClass('fa-angle-right')
                        .addClass('fa-angle-down');
                }).on('hide.bs.collapse', function(){
                    $(this).prev('.list-group-item').removeClass('active');

                    $(this).prev('.list-group-item').find('.icon-sys')
                        .removeClass('fa-angle-down')
                        .addClass('fa-angle-right');
                });
            }, config.dom_delay);
        });
    }

    //获取多选参数
    this.getSelectParam     = function(paramName){
        var param = '';
        $('[name = "'+paramName+'"').each(function(index){
            if($(this).prop('checked')){
                param   += '&' + paramName + '=' + $(this).val();
            }
        });
        
        return param;
    }

    //启动全选按钮
    this.startAllCheck    = function(){
        $timeout(function(){
            $('.check-all').click(function(){
                $(this).closest('.wrap-checkbox')
                    .find('input[type="checkbox"]')
                    .prop('checked', $(this).prop('checked'));
            });
        }, config.dom_delay);
    }

    //提示信息
    this.doAlert   = function(msg) {
        alert(msg);
    }

    //启动分页
    this.startPage    = function(callback){
        $timeout(function(){
            $('.box-page').find('.num').click(function(){
                var page    = $(this).html();
                callback(page);
            });
            $('.box-page').find('.end').click(function(){
                var page   = $(this).html();
                callback(page);
            });
            $('.box-page').find('.first').click(function(){
                var page   = $(this).html();
                callback(page);
            });
            //上一页
            $('.box-page').find('.prev').click(function(){
                var page = $('.box-page').find('.active').children('span').html();
                page    = parseInt(page) - 1;
                callback(page);
            });
            //下一页
            $('.box-page').find('.next').click(function(){
                var page = $('.box-page').find('.active').children('span').html();
                page    = parseInt(page) + 1;
                callback(page);
            })
        },config.dom_delay);
    }

    //打开链接
    this.openUrl   = function(url){
        url     = url.replace('?', '/');
        url     = url.replace('=', '/');
        $location.path(url);
    }

    //错误处理
    this.dealError   = function(info){
        console.log(info);
        switch(info.code){
            case 102:
                window.open('./login.html', '_self');
            break;
            case -502:
                window.open('./login.html', '_self');
            break;
            // case -505:
            //     window.open('./login.html', '_self');
            // break;
            default:
                that.doAlert(info.data);
            break;
        }
    } 

    //图标创建
    this.makeChart  = function(dom, title, xAxis, yTitle, series){
        var json = {};
        json.title = title = {
            text: '<b>'+title+'</b>'   
        };
        json.subtitle = subtitle = {
            text: false
        }
        json.xAxis = {
            categories: xAxis
        };
        json.yAxis = {
            title: {
                text: yTitle
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        };   
        json.tooltip = {valueSuffix: ''}
        json.legend = {};
        json.series = series;

        $(dom).highcharts(json);
    }
});
